package action;


import java.util.ArrayList;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Item;
import com.opensymphony.xwork2.ActionSupport;
import dao.CartManager;
import org.apache.struts2.ServletActionContext;

public class AddToCart extends ActionSupport {

	public String addToCart(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpSession session = ServletActionContext.getRequest().getSession();
		String goodsId = request.getParameter("goodsId");
		CartManager cartManager = new CartManager();

		ArrayList<Item> cart = (ArrayList<Item>) session.getAttribute("cart");
		if(cart != null){
			cartManager.setCart(cart);
			cartManager.addToCart(goodsId, 1);
		}else{
			cart = new ArrayList<Item>();
			cartManager.setCart(cart);
			cartManager.addToCart(goodsId, 1);
		}
		for (int i = 0; i < cart.size(); i++){
			System.out.println(cart.get(i).getGoods().getGoodsName());
		}
		session.setAttribute("cart", cartManager.getCart());

		return SUCCESS;
	}
}
